package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.ProductProgressPrice;
import com.ruoyi.inventory.service.IProductProgressPriceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 产品工序价格Controller
 * 
 * @author xwj
 * @date 2024-08-06
 */
@RestController
@RequestMapping("/inventory/productProgressPrice")
@Api(tags = "产品工序价格")
public class ProductProgressPriceController extends BaseController {
    @Autowired
    private IProductProgressPriceService productProgressPriceService;

    /**
     * 查询产品工序价格列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody ProductProgressPrice productProgressPrice) {
        startPage();
        List<ProductProgressPrice> list = productProgressPriceService.selectProductProgressPriceList(productProgressPrice);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody ProductProgressPrice productProgressPrice) {
        startOrderBy();
        List<ProductProgressPrice> list = productProgressPriceService.selectProductProgressPriceList(productProgressPrice);
        return success(list);
    }

    @GetMapping("/listAllPriceByProduct")
    @ApiOperation("根据产品id查询")
    public AjaxResult listAllPriceByProduct(Long progressId, Long categoryId) {
        List<ProductProgressPrice> list = productProgressPriceService.listAllPriceByProduct(progressId, categoryId);
        return success(list);
    }



    /**
     * 导出产品工序价格列表
     */
    @Log(title = "产品工序价格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, ProductProgressPrice productProgressPrice) {
        List<ProductProgressPrice> list = productProgressPriceService.selectProductProgressPriceList(productProgressPrice);
        ExcelUtil<ProductProgressPrice> util = new ExcelUtil<ProductProgressPrice>(ProductProgressPrice.class);
        util.exportExcel(response, list, "产品工序价格数据");
    }

    /**
     * 获取产品工序价格详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(productProgressPriceService.selectProductProgressPriceById(id));
    }

    @PostMapping("/batchSaveProductPrice")
    @ApiOperation("批量保存更新产品工序价格")
    public AjaxResult batchSaveProductPrice(@RequestBody List<ProductProgressPrice> list) {
        SysUser user = getLoginUser().getUser();

        productProgressPriceService.batchSaveProductPrice(list, user);
        return success();
    }

    /**
     * 新增产品工序价格
     */
    @Log(title = "产品工序价格", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody ProductProgressPrice productProgressPrice) {
        SysUser user = getLoginUser().getUser();
        if(productProgressPrice.getId() == null) {
        productProgressPrice.setCreateId(user.getUserId());
            productProgressPrice.setCreateTime(DateUtils.getNowDate());
            productProgressPrice.setCreateBy(user.getNickName());
            productProgressPriceService.insertProductProgressPrice(productProgressPrice);
        } else {
            productProgressPrice.setUpdateId(user.getUserId());
            productProgressPrice.setUpdateTime(DateUtils.getNowDate());
            productProgressPrice.setUpdateBy(user.getNickName());
            productProgressPriceService.updateProductProgressPrice(productProgressPrice);
        }

        return success(productProgressPrice);
    }

    /**
     * 修改产品工序价格
     */
    @Log(title = "产品工序价格", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody ProductProgressPrice productProgressPrice) {
        SysUser user = getLoginUser().getUser();
                productProgressPrice.setUpdateId(user.getUserId());
                productProgressPrice.setUpdateTime(DateUtils.getNowDate());
                productProgressPrice.setUpdateBy(user.getNickName());
        return success(productProgressPriceService.updateProductProgressPrice(productProgressPrice));
    }

    /**
     * 删除产品工序价格
     */
    @Log(title = "产品工序价格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productProgressPriceService.deleteProductProgressPriceByIds(ids));
    }

    @Log(title = "产品工序价格", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody ProductProgressPrice productProgressPrice) {
        int num = productProgressPriceService.delete(productProgressPrice);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<ProductProgressPrice>> queryPage(@RequestBody ProductProgressPrice productProgressPrice) {
        Pages<ProductProgressPrice> page = productProgressPriceService.queryPage(productProgressPrice);
        return ResponseModel.of(page);
    }
}
